package com.ruoyi.quartz.task;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.parttime.chat.service.IChatServiceService;
import com.ruoyi.parttime.domain.ApplyForParrtime;
import com.ruoyi.parttime.domain.ParttimePublish;
import com.ruoyi.parttime.service.IApplyForParrtimeService;
import com.ruoyi.parttime.service.IParttimePublishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class MyTask {
    @Autowired
    private IApplyForParrtimeService iApplyForParrtimeService;

    @Autowired
    private IParttimePublishService parttimePublishService;

    @Autowired
    private IChatServiceService chatServiceService;

    @Autowired
    private RedisCache redisCache;

//    @Scheduled(cron = "0 0/30 * * * ?")//每30分钟执行一次
    public void processFriendPlease() {
        log.info("处理兼职申请");
        LocalDateTime time = LocalDateTime.now().plusMinutes(-10080);//处理超过一个礼拜的兼职请求
        log.info("后退时间" + time);
        List<ApplyForParrtime> applyForParrtimeList = iApplyForParrtimeService.getByTimeList(0, time);
        if (applyForParrtimeList != null && applyForParrtimeList.size() > 0) {
            for (ApplyForParrtime apply : applyForParrtimeList) {
                apply.setApplyForStatus(2);
                iApplyForParrtimeService.updateApplyForParrtime(apply);
            }
        }
    }

//    @Scheduled(cron = "0 0/1 * * * ?")//每一分钟执行一次
    public void HandlePartTime() {
        String key = Constants.APPLY_FOR_KEY;
        if (redisCache.getCacheMap(key) != null) {
            //获取数据在缓存中的剩余时间
            Long time = redisCache.getExpire(key);
            log.info("正在执行，是否需要对兼职申请次数进行增加操作" + time);
            //如果剩余时间小于60秒，则进行修改操作
            if (0 < time && time <= 60) {
                //从缓存中取出值
                Map<String, ParttimePublish> parttimeMap = redisCache.getCacheMap(key);
                //进行修改
                Collection<ParttimePublish> values = parttimeMap.values();
                List<ParttimePublish> list = new ArrayList<>(values);
                //批量修改
                parttimePublishService.batchUpdateParttimePublish(list);
//                parttimePublishService.updateParttimePublish(parttimeMap.get("ParttimePublish"));
                //删除缓存数据
                redisCache.deleteObject(key);
            }
        }
    }
}
